package com.example.recordingpen_datacollection.controller;


import com.example.recordingpen_datacollection.Utils.EncryptionUtils;
import com.example.recordingpen_datacollection.entity.GetdosSta;
import com.example.recordingpen_datacollection.entity.Query.GetdosStaQue;
import com.example.recordingpen_datacollection.entity.Vo.ResHtmlVo;

import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.result.exceptionhandler.JWException;
import com.example.recordingpen_datacollection.service.GetdosStaService;
import com.example.recordingpen_datacollection.service.impl.VideoHttpRequestHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2024-09-02
 */
@Api(tags = "会议信息")
@RestController
@RequestMapping("/getdos-sta")
public class GetdosStaController {
    @Autowired
    private GetdosStaService getdosStaService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private VideoHttpRequestHandler videoHttpRequestHandler;

    @ApiOperation(value = "查询设备的信息")
    @PostMapping("getDossta")
    public RLIST getDossta(@RequestBody GetdosStaQue getdosStaQue , HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 36 ){
            return RLIST.error().message("无权操作");
        }
        if ((getdosStaQue.getPageNo() == null || getdosStaQue.getPageNo() <=0) ){
            getdosStaQue.setPageNo(1);
        }
        if ((getdosStaQue.getPageSize() == null || getdosStaQue.getPageSize() <=0) ){
            getdosStaQue.setPageSize(10);
        }
        Integer value = (Integer) redisService.getValue(token);

        RLIST rlist = getdosStaService.getDosstaPage(getdosStaQue,value);
        return rlist;
    }

    @ApiOperation(value = "获取结果 生成html")
    @PutMapping("/getResHtml")
    public RLIST getResHtml(@RequestBody ResHtmlVo resHtmlVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 36 ){
            return RLIST.error().message("无权操作");
        }
        if(resHtmlVo.getId() == null || resHtmlVo.getId() <=0 && resHtmlVo.getServerType() == null || resHtmlVo.getServerType() <=0){
            return RLIST.error().message("id异常");
        }
        RLIST rlist = getdosStaService.getResHtml(resHtmlVo);
        return rlist;
    }

    @ApiOperation(value = "在线查看")
    @GetMapping("/player")
    public void getPlayResource(HttpServletRequest request,String file,  HttpServletResponse response) throws Exception {
        String decrypt = EncryptionUtils.decrypt(file);
        if(decrypt == null ){
            return;
        }
        Integer  fid = Integer.valueOf(decrypt);
        if(fid == null || fid <=0){
            return;
        }
        GetdosSta byId = getdosStaService.getById(fid);


        if(byId == null){
            throw new JWException(400,"文件名异常导致获取失败");
        }

        String url = byId.getHttpUrl();
        Path path = Paths.get(url);
        if (Files.exists(path)) {
            //   获取对应的类型 类型
            String mimeType = Files.probeContentType(path);
            System.err.println(mimeType);
            if (!StringUtils.isEmpty(mimeType)) {
                //设置内容类型  video/mp4
                response.setContentType(mimeType);
            }
            //设置属性
            request.setAttribute(VideoHttpRequestHandler.ATTR_FILE, path);
            // 处理请求
            videoHttpRequestHandler.handleRequest(request, response);
        } else {
            //设定状态码
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            //编码字符集
            response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        }
    }

}

